/**
  ******************************************************************************
  * @file    w32f006_acmp.c
  * @author  WeiLink Technology
  * @version V1.0.0
  * @date    09/01/2020
  * @brief   This file provides firmware functions to manage the following 
  *          functionalities of the Analog Comparator (ACMP) peripheral:
  *           + Comparators configuration
  *
  ******************************************************************************  
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, WeiLink Technology SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT WeiLink Technology</center></h2>
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "w32f006_acmp.h"
#include "w32f006_rcc.h"

/** @addtogroup W32F006_StdPeriph_Driver
  * @{
  */

/** @defgroup ACMP 
  * @brief ACMP driver modules
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define	ACMP_IT_ACTION_FALLING                      ((uint32_t)0x00010000)
#define	ACMP_IT_ACTION_RISING                       ((uint32_t)0x00020000)
#define	ACMP_IT_ACTION_DOUBLE                       ((uint32_t)0x00000000)

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/** @defgroup ACMP_Private_Functions
  * @{
  */

/**
  * @brief  Deinitializes ACMP peripheral registers to their default reset values.
  * @param  None
  * @retval None
  */
void ACMP_DeInit(void)
{
	RCC_APBPeriphResetCmd(RCC_PRESETCTRL_ACMP, ENABLE);
	RCC_APBPeriphResetCmd(RCC_PRESETCTRL_ACMP, DISABLE);
}

/**
  * @brief  Initializes the ACMP peripheral according to the specified parameters
  *         in the ACMP_InitStruct.
  * @param  ACMPx: Select the ACMP peripheral. 
  * @param  ACMP_InitStruct: pointer to an ACMP_InitTypeDef structure that contains 
  *         the configuration information for the specified ACMP peripheral.
  * @retval None
  */
void ACMP_Init(uint32_t ACMPx, ACMP_InitTypeDef* ACMP_InitStruct)
{
    assert_param(IS_ACMP_NEGATIVE_SELECT(ACMP_InitStruct->ACMP_NegativeSelect));
    assert_param(IS_ACMP_POSITIVE_SELECT(ACMP_InitStruct->ACMP_PositiveSelect));
    assert_param(IS_ACMP_OUTPUT_STATE(ACMP_InitStruct->ACMP_Output));
    assert_param(IS_ACMP_HYSTERESIS(ACMP_InitStruct->ACMP_Hysteresis));
    assert_param(IS_ACMP_ALL_PERIPH(ACMPx));
    
    if (ACMPx == ACMP0)
    {
        ACMP->CTRL0 = ACMP_InitStruct->ACMP_PositiveSelect | \
                      ACMP_InitStruct->ACMP_NegativeSelect | \
                      ACMP_InitStruct->ACMP_Output | \
                      ACMP_InitStruct->ACMP_Hysteresis;
    }
    else if (ACMPx == ACMP1)
    {
        ACMP->CTRL1 = ACMP_InitStruct->ACMP_PositiveSelect | \
                      ACMP_InitStruct->ACMP_NegativeSelect | \
                      ACMP_InitStruct->ACMP_Output | \
                      ACMP_InitStruct->ACMP_Hysteresis;
    }
    
    ACMP->CMPCLKDIV = ACMP_InitStruct->ACMP_CLKDIV;
}

/**
  * @brief  Enables or disables ACMP.
  * @param  ACMPx: Select the ACMP peripheral. 
  *   This parameter can be one of the following values: ACMPx,x:[0:1].
  * @param  NewState: new state of ACMP.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void ACMP_Cmd(uint32_t ACMPx, FunctionalState NewState)
{
	/* Check the parameters */
    assert_param(IS_ACMP_ALL_PERIPH(ACMPx));
    assert_param(IS_FUNCTIONAL_STATE(NewState));
    
    if (NewState != DISABLE)
    {
        if (ACMPx == ACMP0)
            ACMP->CTRL0 |= ACMP_CTRL_CMP_EN;
        else if (ACMPx == ACMP1)
            ACMP->CTRL1 |= ACMP_CTRL_CMP_EN;
    }
    else
    {
        if (ACMPx == ACMP0)
            ACMP->CTRL0 &= ~ACMP_CTRL_CMP_EN;
        else if (ACMPx == ACMP1)
            ACMP->CTRL1 &= ~ACMP_CTRL_CMP_EN;
    }
}

/**
  * @brief  Config sync compare.
  * @param  ACMPx: Select the ACMP peripheral. 
  *   This parameter can be one of the following values: ACMPx,x:[0:1].
  * @param  TRGS: This parameter can be :
  *            @arg ACMP_SYNC_CMP_TRGS_NONE
  *            @arg ACMP_SYNC_CMP_TRGS_PWM0
  *            @arg ACMP_SYNC_CMP_TRGS_PWM1
  *            @arg ACMP_SYNC_CMP_TRGS_PWM2
  * @param  TRGPOL: This parameter can be :
  *            @arg ACMP_SYNC_CMP_TRGPOL_INVALID
  *            @arg ACMP_SYNC_CMP_TRGPOL_AVAILABILITY
  * @retval None
  */
void ACMP_SyncCompareConfig(uint32_t ACMPx, uint32_t TRGS, uint32_t TRGPOL)
{
	/* Check the parameters */
    assert_param(IS_ACMP_ALL_PERIPH(ACMPx));
    assert_param(IS_ACMP_SYNC_CMP_TRGS(TRGS));
    assert_param(IS_ACMP_SYNC_CMP_TRGPOL(TRGPOL));
    
    if (ACMPx == ACMP0)
    {
        ACMP->ADV_CTRL0 |= (TRGS << 0) | (TRGPOL << 2);
    }
    else if (ACMPx == ACMP1)
    {
        ACMP->ADV_CTRL0 |= (TRGS << 4) | (TRGPOL << 6);
    }
}

/**
  * @brief  Configure ACMP filter 1.
  * @param  ACMPx: Select the ACMP peripheral. 
  *   This parameter can be one of the following values: ACMPx,x:[0:1].
  * @param  Para: This parameter can be :
  *            @arg ACMP_FILTER1_PARA_32
  *            @arg ACMP_FILTER1_PARA_64
  *            @arg ACMP_FILTER1_PARA_128
  *            @arg ACMP_FILTER1_PARA_256
  *            @arg ACMP_FILTER1_PARA_512
  *            @arg ACMP_FILTER1_PARA_1024
  *            @arg ACMP_FILTER1_PARA_2048
  *            @arg ACMP_FILTER1_PARA_4096
  * @param  NewState: new state of ACMP filter 1.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void ACMP_Filter1Config(uint32_t ACMPx, uint32_t Para, FunctionalState NewState)
{
    uint32_t tmp_reg;
    
	/* Check the parameters */
    assert_param(IS_ACMP_ALL_PERIPH(ACMPx));
    assert_param(IS_ACMP_FILTER1_PARA(Para));
    assert_param(IS_FUNCTIONAL_STATE(NewState));
    
    tmp_reg = ACMP->ADV_CTRL1;
    
    if (ACMPx == ACMP0)
    {
        tmp_reg &= ~0x70;
        tmp_reg |= Para<<4;
        if (NewState != DISABLE)
        {
            tmp_reg |= 0x80;
        }
        else
        {
            tmp_reg &= ~0x80;
        }
        
        ACMP->ADV_CTRL1 = tmp_reg;
    }
    else if (ACMPx == ACMP1)
    {
        tmp_reg &= ~0x7000;
        tmp_reg |= Para<<12;
        if (NewState != DISABLE)
        {
            tmp_reg |= 0x8000;
        }
        else
        {
            tmp_reg &= ~0x8000;
        }
        
        ACMP->ADV_CTRL1 = tmp_reg;
    }
}

/**
  * @brief  Configure ACMP filter 2.
  * @param  ACMPx: Select the ACMP peripheral. 
  *   This parameter can be one of the following values: ACMPx,x:[0:1].
  * @param  Para: This parameter can be :
  *            @arg ACMP_FILTER2_PARA_8
  *            @arg ACMP_FILTER2_PARA_16
  *            @arg ACMP_FILTER2_PARA_32
  *            @arg ACMP_FILTER2_PARA_64
  *            @arg ACMP_FILTER2_PARA_128
  *            @arg ACMP_FILTER2_PARA_256
  *            @arg ACMP_FILTER2_PARA_512
  *            @arg ACMP_FILTER2_PARA_1024
  * @param  NewState: new state of ACMP filter 2.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void ACMP_Filter2Config(uint32_t ACMPx, uint32_t Para, FunctionalState NewState)
{
    uint32_t tmp_reg;
    
	/* Check the parameters */
    assert_param(IS_ACMP_ALL_PERIPH(ACMPx));
    assert_param(IS_ACMP_FILTER2_PARA(Para));
    assert_param(IS_FUNCTIONAL_STATE(NewState));
    
    tmp_reg = ACMP->ADV_CTRL1;
    
    if (ACMPx == ACMP0)
    {
        tmp_reg &= ~0x7;
        tmp_reg |= Para<<0;
        if (NewState != DISABLE)
        {
            tmp_reg |= 0x8;
        }
        else
        {
            tmp_reg &= ~0x8;
        }
        
        ACMP->ADV_CTRL1 = tmp_reg;
    }
    else if (ACMPx == ACMP1)
    {
        tmp_reg &= ~0x700;
        tmp_reg |= Para<<8;
        if (NewState != DISABLE)
        {
            tmp_reg |= 0x800;
        }
        else
        {
            tmp_reg &= ~0x800;
        }
        
        ACMP->ADV_CTRL1 = tmp_reg;
    }
}

/**
  * @brief  Configure ACMP DEB filter.
  * @param  ACMPx: Select the ACMP peripheral. 
  *   This parameter can be one of the following values: ACMPx,x:[0:1].
  * @param  Para: This parameter can be [0:100].
  * @param  NewState: new state of ACMP DEB filter.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void ACMP_FilterDEBConfig(uint32_t ACMPx, uint32_t Para, FunctionalState NewState)
{
    uint32_t tmp_reg;
    
	/* Check the parameters */
    assert_param(IS_ACMP_ALL_PERIPH(ACMPx));
    assert_param(IS_ACMP_FILTERDEB_PARA(Para));
    assert_param(IS_FUNCTIONAL_STATE(NewState));

    tmp_reg = ACMP->ADV_CTRL2;
    
    if (ACMPx == ACMP0)
    {
        tmp_reg &= ~0xFF00;
        tmp_reg |= Para<<8;
        if (NewState != DISABLE)
        {
            tmp_reg |= 0x1;
        }
        else
        {
            tmp_reg &= ~0x1;
        }
        
        ACMP->ADV_CTRL2 = tmp_reg;
    }
    else if (ACMPx == ACMP1)
    {
        tmp_reg &= ~0xFF0000;
        tmp_reg |= Para<<16;
        if (NewState != DISABLE)
        {
            tmp_reg |= 0x2;
        }
        else
        {
            tmp_reg &= ~0x2;
        }
        
        ACMP->ADV_CTRL2 = tmp_reg;
    }
}

/**
  * @brief  Configure ACMP filter select.
  * @param  ACMPx: Select the ACMP peripheral. 
  *   This parameter can be one of the following values: ACMPx,x:[0:1].
  * @param  Select: This parameter can be :
  *            @arg ACMP_FLT_SELECT_CMP_OUT
  *            @arg ACMP_FLT_SELECT_EXTERNAL_PIN
  * @retval None
  */
void ACMP_FLTSelect(uint32_t ACMPx, uint32_t Select)
{
	/* Check the parameters */
    assert_param(IS_ACMP_ALL_PERIPH(ACMPx));
    assert_param(IS_ACMP_FLT_SELECT(Select));

    if (ACMPx == ACMP0)
    {
        if (Select == ACMP_FLT_SELECT_CMP_OUT)
            ACMP->ADV_CTRL3 &= ~ACMP_FLT_SELECT_EXTERNAL_PIN;
        else
            ACMP->ADV_CTRL3 |= ACMP_FLT_SELECT_EXTERNAL_PIN;
    }
    else if (ACMPx == ACMP1)
    {
        if (Select == ACMP_FLT_SELECT_CMP_OUT)
            ACMP->ADV_CTRL3 &= ~(ACMP_FLT_SELECT_EXTERNAL_PIN << 1);
        else
            ACMP->ADV_CTRL3 |= (ACMP_FLT_SELECT_EXTERNAL_PIN << 1);
    }
}

/**
  * @brief  Configure Motor PWM Output.
  * @param  PWM: This parameter can be :
  *            @arg ACMP_PWMOUT_SELECT_PWM0
  *            @arg ACMP_PWMOUT_SELECT_PWM1
  *            @arg ACMP_PWMOUT_SELECT_PWM2
  * @param  Select: This parameter can be :
  *            @arg ACMP_PWMOUT_SELECT_ALWAYS
  *            @arg ACMP_PWMOUT_SELECT_ACMP0
  *            @arg ACMP_PWMOUT_SELECT_ACMP1
  * @retval None
  */
void ACMP_PWMOutSelect(uint32_t PWM, uint32_t Select)
{
    uint32_t tmp_reg;
    
	/* Check the parameters */
    assert_param(IS_ACMP_PWMOUT_PWM(PWM));
    assert_param(IS_ACMP_PWMOUT_SELECT(Select));

    tmp_reg = ACMP->ADV_CTRL3;
    
    if (PWM == ACMP_PWMOUT_SELECT_PWM0)
    {
        tmp_reg &= ~ACMP_PWMOUT_SELECT_PWM0;
        tmp_reg |= Select << 4;
    }
    else if (PWM == ACMP_PWMOUT_SELECT_PWM1)
    {
        tmp_reg &= ~ACMP_PWMOUT_SELECT_PWM1;
        tmp_reg |= Select << 6;
    }
    else
    {
        tmp_reg &= ~ACMP_PWMOUT_SELECT_PWM2;
        tmp_reg |= Select << 8;
    }
    
    ACMP->ADV_CTRL3 = tmp_reg;
}

/**
  * @brief  Configure ACMP res division.
  * @param  Source: res division source.
  *   This parameter can be: ACMP_RES_DIV_SOURCE_VDD or ACMP_RES_DIV_SOURCE_ADC_VREF.
  * @param  Div:
  *   This parameter can be: [0:63].
  * @retval None
  */
void ACMP_RESDIVConfig(uint32_t Source, uint32_t Div)
{
    uint32_t tmp_reg;

	/* Check the parameters */
    assert_param(IS_ACMP_RES_DIV_SOURCE(Source));
    assert_param(IS_ACMP_RES_DIV(Div));

    tmp_reg = ACMP->ADV_CTRL4;
    
    /* source */
    tmp_reg &= ~ACMP_RES_DIV_SOURCE_ADC_VREF;
    tmp_reg |= Source;
    
    /* div */
    tmp_reg &= ~0x3F00;
    tmp_reg |= (Div)<<8;
    
    ACMP->ADV_CTRL4 = tmp_reg;
}

/**
  * @brief  Enables or disables ACMP res division.
  * @param  NewState: new state of ACMP.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void ACMP_RESDIVCmd(FunctionalState NewState)
{
	/* Check the parameters */
    assert_param(IS_FUNCTIONAL_STATE(NewState));
    
    if (NewState != DISABLE)
    {
        ACMP->ADV_CTRL4 |= 0x10UL;
    }
    else
    {
        ACMP->ADV_CTRL4 &= ~0x10UL;
    }
}

/**
  * @brief  Enables or disables the specified ACMP interrupts.
  * @param  ACMPx: Select the ACMP peripheral. 
  *   This parameter can be one of the following values: ACMPx,x:[0:1].
  * @param  ACMP_IT: specifies the ACMP interrupt sources to be enabled or disabled.
  *   This parameter can be one of the following values:
  *     @arg ACMP_IT_ACTION_FALLING
  *     @arg ACMP_IT_ACTION_RISING
  *     @arg ACMP_IT_ACTION_DOUBLE
  * @param  NewState: new state of the specified ACMP interrupts.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void ACMP_ITConfig(uint32_t ACMPx, uint32_t ACMP_IT, FunctionalState NewState)
{
    uint32_t tmp_reg;
    
	/* Check the parameters */
    assert_param(IS_ACMP_ALL_PERIPH(ACMPx));
    assert_param(IS_ACMP_IT_ACTION(ACMP_IT));
    assert_param(IS_FUNCTIONAL_STATE(NewState));
    
    if (NewState != DISABLE)
    {
        if (ACMPx == ACMP0)
        {
            tmp_reg = ACMP->CTRL0;
            tmp_reg &= ~ACMP_CTRL_CMP_EDGE_SEL_MASK;
            tmp_reg |= ACMP_IT;
            tmp_reg |= ACMP_CTRL_CMP_IE;
            ACMP->CTRL0 = tmp_reg;
        }
        else if (ACMPx == ACMP1)
        {
            tmp_reg = ACMP->CTRL1;
            tmp_reg &= ~ACMP_CTRL_CMP_EDGE_SEL_MASK;
            tmp_reg |= ACMP_IT;
            tmp_reg |= ACMP_CTRL_CMP_IE;
            ACMP->CTRL1 = tmp_reg;
        }
    }
    else
    {
        if (ACMPx == ACMP0)
        {
            tmp_reg = ACMP->CTRL0;
            tmp_reg &= ~ACMP_CTRL_CMP_EDGE_SEL_MASK;
            tmp_reg |= ACMP_IT;
            tmp_reg &= ~ACMP_CTRL_CMP_IE;
            ACMP->CTRL0 = tmp_reg;
        }
        else if (ACMPx == ACMP1)
        {
            tmp_reg = ACMP->CTRL1;
            tmp_reg &= ~ACMP_CTRL_CMP_EDGE_SEL_MASK;
            tmp_reg |= ACMP_IT;
            tmp_reg &= ~ACMP_CTRL_CMP_IE;
            ACMP->CTRL1 = tmp_reg;
        }
    }
}

/**
  * @brief  Get ACMP result.
  * @param  ACMPx: Select the ACMP peripheral. 
  *   This parameter can be one of the following values: ACMPx,x:[0:1].
  * @retval None
  */
FlagStatus ACMP_GetResult(uint32_t ACMPx)
{
	FlagStatus bitstatus = RESET;

	/* Check the parameters */
    assert_param(IS_ACMP_ALL_PERIPH(ACMPx));
    
    if (ACMPx == ACMP0)
    {
        if (ACMP->STATUS & ACMP_STATUS_RESULT0)
            bitstatus = SET;
    }
    else if (ACMPx == ACMP1)
    {
        if (ACMP->STATUS & ACMP_STATUS_RESULT1)
            bitstatus = SET;
    }

	return bitstatus;
}

/**
  * @brief  Checks whether ACMP interrupt has occurred or not.
  * @param  ACMPx: Select the ACMP peripheral. 
  *   This parameter can be one of the following values: ACMPx,x:[0:1].
  * @retval The new state of ACMP (SET or RESET).
  */
ITStatus ACMP_GetITStatus(uint32_t ACMPx)
{
	ITStatus bitstatus = RESET;
	uint32_t enablestatus = 0;

	/* Check the parameters */
    assert_param(IS_ACMP_ALL_PERIPH(ACMPx));
    
    if (ACMPx == ACMP0)
    {
        if (ACMP->CTRL0 & ACMP_CTRL_CMP_IE)
            enablestatus = 1;
        
        if (enablestatus && (ACMP->STATUS & ACMP_STATUS_INT_FLAG0))
            bitstatus = SET;
    }
    else if (ACMPx == ACMP1)
    {
        if (ACMP->CTRL1 & ACMP_CTRL_CMP_IE)
            enablestatus = 1;
        
        if (enablestatus && (ACMP->STATUS & ACMP_STATUS_INT_FLAG1))
            bitstatus = SET;
    }
    
	return  bitstatus;
}

/**
  * @brief  Clears the ACMP's interrupt pending bits.
  * @param  ACMPx: Select the ACMP peripheral. 
  *   This parameter can be one of the following values: ACMPx,x:[0:1].
  * @retval None
  */
void ACMP_ClearITPendingBit(uint32_t ACMPx)
{
	/* Check the parameters */
    assert_param(IS_ACMP_ALL_PERIPH(ACMPx));
    
    if (ACMPx == ACMP0)
    {
        ACMP->STATUS |= ACMP_STATUS_INT_FLAG0;
    }
    else if (ACMPx == ACMP1)
    {
        ACMP->STATUS |= ACMP_STATUS_INT_FLAG1;
    }
}

/**
  * @brief  Select OPA.
  * @param  ACMPx: Select the ACMP peripheral. 
  *   This parameter can be one of the following values: ACMPx,x:[0:1].
  * @param  OPAx: Select the OPA peripheral. 
  *   This parameter can be one of the following values: OPAx,x:[0:1].
  * @param  NewState: new state of OPA.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void ACMP_SelectOPA(uint32_t ACMPx, uint32_t OPAx, FunctionalState NewState)
{
	/* Check the parameters */
    assert_param(IS_ACMP_ALL_PERIPH(ACMPx));
    assert_param(IS_OPA_ALL_PERIPH(OPAx));
    assert_param(IS_FUNCTIONAL_STATE(NewState));

    if (ACMPx == ACMP0)
    {
        if (OPAx == OPA0)
        {
            ACMP->CTRL0 &= ~ACMP_CTRL_CMP_OPA_SELECT;
        }
        else
        {
            ACMP->CTRL0 |= ACMP_CTRL_CMP_OPA_SELECT;
        }
        
        if (NewState != DISABLE)
        {
            ACMP->CTRL0 |= ACMP_CTRL_CMP_OPA_ENABLE;
        }
        else
        {
            ACMP->CTRL0 &= ~ACMP_CTRL_CMP_OPA_ENABLE;
        }
    }
    else if (ACMPx == ACMP1)
    {
        if (OPAx == OPA0)
        {
            ACMP->CTRL1 &= ~ACMP_CTRL_CMP_OPA_SELECT;
        }
        else
        {
            ACMP->CTRL1 |= ACMP_CTRL_CMP_OPA_SELECT;
        }
        
        if (NewState != DISABLE)
        {
            ACMP->CTRL1 |= ACMP_CTRL_CMP_OPA_ENABLE;
        }
        else
        {
            ACMP->CTRL1 &= ~ACMP_CTRL_CMP_OPA_ENABLE;
        }
    }
}

/**
  * @brief  Configure OPA.
  * @param  OPAx: Select the OPA peripheral. 
  *   This parameter can be one of the following values: OPAx,x:[0:1].
  * @param  mode: OPA mode
  *   This parameter can be:
  *     OPA_EXT_RES
  *     OPA_GAIN_1X
  *     OPA_GAIN_2X
  *     OPA_GAIN_3X
  *     OPA_GAIN_4X
  *     OPA_GAIN_6X
  *     OPA_GAIN_8X
  *     OPA_GAIN_10X
  * @retval None
  */
void OPA_Config(uint32_t OPAx, uint32_t Gain)
{
    uint32_t tmp_reg;
    
	/* Check the parameters */
    assert_param(IS_OPA_ALL_PERIPH(OPAx));
    assert_param(IS_OPA_GAIN(Gain));

    tmp_reg = ACMP->OPA_CTRL;
    
    if (OPAx == OPA0)
    {
        tmp_reg &= 0xFFFFFFF1;
        tmp_reg |= Gain << 1;
        ACMP->OPA_CTRL = tmp_reg;
    }
    else if (OPAx == OPA1)
    {
        tmp_reg &= 0xFFFFF1FF;
        tmp_reg |= Gain << 9;
        ACMP->OPA_CTRL = tmp_reg;
    }
}

/**
  * @brief  Enables or disables OPA.
  * @param  OPAx: Select the OPA peripheral. 
  *   This parameter can be one of the following values: OPAx,x:[0:1].
  * @param  NewState: new state of OPA.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void OPA_Cmd(uint32_t OPAx, FunctionalState NewState)
{
	/* Check the parameters */
    assert_param(IS_OPA_ALL_PERIPH(OPAx));
    assert_param(IS_FUNCTIONAL_STATE(NewState));
    
    if (NewState != DISABLE)
    {
        if (OPAx == OPA0)
        {
            ACMP->OPA_CTRL &= 0xFFFFFFFE;
        }
        else if (OPAx == OPA1)
        {
            ACMP->OPA_CTRL &= 0xFFFFFEFF;
        }
    }
    else
    {
        if (OPAx == OPA0)
        {
            ACMP->OPA_CTRL |= 0x1;
        }
        else if (OPAx == OPA1)
        {
            ACMP->OPA_CTRL |= 0x1 << 8;
        }
    }
}


/**
  * @}
  */ 

/**
  * @}
  */ 

/**
  * @}
  */ 

/************************ (C) COPYRIGHT WeiLink Technology *****END OF FILE****/

